//
//  ApproveList.swift
//  DesignCode
//
//  Created by 李澄 on 2020/11/20.
//  Copyright © 2020 Mithun. All rights reserved.
//

import SwiftUI

struct ApproveList: View {
    var appropves = applyData
    @ObservedObject var store = ApproveStore(updates: approveData)

    func move(from source: IndexSet, to destination: Int) {
       store.approves.swapAt(source.first!, destination)
    }

    var body: some View {
       NavigationView {
          List {
             ForEach(store.approves) { item in
                NavigationLink(destination: ApproveDetail(image: item.image, applyId: item.applyId, date: item.date, status: item.status, isApproved: item.isApproved, suggestion: item.suggestion, personId: item.personId)) {
                    HStack(spacing: 13.0) {
                       Image(item.image)
                          .resizable()
                          .aspectRatio(contentMode: .fit)
                          .frame(width: 80, height: 80)
                          .background(Color("background"))
                          .cornerRadius(20)

                       VStack(alignment: .leading) {
                          Text("申请编号：\(item.applyId)")
                             .font(.headline)

                          Text("审批人编号：\(item.personId)")
                             .lineLimit(2)
                             .lineSpacing(4)
                             .font(.subheadline)
                             .frame(height: 50.0)

                          Text(item.date)
                             .font(.caption)
                             .fontWeight(.bold)
                             .foregroundColor(.gray)
                       }
                        
                        Spacer()
                        
                        Text(item.isApproved)
                            .foregroundColor(.orange)
                            .font(.headline)
                            .bold()
                            
                    }

                }
                .padding(.vertical, 8.0)
                
             }
             .onDelete { index in
                self.store.approves.remove(at: index.first!)
             }
             .onMove(perform: move)
          }
          .navigationBarTitle(Text("审批记录"))
          
       }
    }
}

struct Approve: Identifiable {
    var id = UUID()
    var image: String
    var personId = ""
    var applyId = ""
    var date = ""
    var status = ""
    var isApproved = ""
    var suggestion = ""
}

struct ApproveList_Previews: PreviewProvider {
    static var previews: some View {
        ApproveList()
    }
}

let approveData = [
    Approve(image: "Illustration1", personId: "001", applyId: "1", date: "JUN 26 - 28", status: "副总经理审批", isApproved: "批准", suggestion: "奶思!"),
    Approve(image: "Illustration2", personId: "002", applyId: "1", date: "JUN 26 - 28", status: "副总经理审批", isApproved: "批准", suggestion: "奶思!"),
    Approve(image: "Illustration3", personId: "003", applyId: "1", date: "JUN 26 - 28", status: "副总经理审批", isApproved: "批准", suggestion: "奶思!"),
    Approve(image: "Illustration4", personId: "004", applyId: "1", date: "JUN 26 - 28", status: "副总经理审批", isApproved: "批准", suggestion: "奶思!"),
    Approve(image: "Illustration5", personId: "006", applyId: "1", date: "JUN 26 - 28", status: "副总经理审批", isApproved: "批准", suggestion: "奶思!")
]

